//
//  YSShoppingCartViewController.m
//  HubayMe1
//
//  Created by apple on 16/12/16.
//  Copyright © 2016年 Global Barter. All rights reserved.
//

#import "YSShoppingCartViewController.h"
#import "YSRecommendedProductsCell.h"

#import "YSWebViewController.h"

static CGFloat const margin = 10;
static NSInteger const cols = 2;
#define wh ((kScreenWidth - (cols - 1) * margin) / cols)


static NSString * const ID = @"RecommendedProductsCell";
@interface YSShoppingCartViewController ()<UITableViewDataSource , UITableViewDelegate,UICollectionViewDataSource,UICollectionViewDelegate>
@property (strong ,nonatomic) UIView *topView;
@property (nonatomic, weak)  UICollectionView *collectionView;
@property(nonatomic,strong)UITableView *tableView;

@end

@implementation YSShoppingCartViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpTableView];
    
    [self setUpTopView];
    //2 设置底部的View
    [self setUpBottomView];
}
-(void)setUpTableView{
    self.tableView = [[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
    self.tableView.tableFooterView.backgroundColor = [UIColor grayColor];
    
    self.tableView.showsVerticalScrollIndicator = NO;
    
    [self.view addSubview:self.tableView];
    self.tableView.dataSource =self;
    self.tableView.delegate = self;
    //[self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([CJShoppingCarCell class]) bundle:nil] forCellReuseIdentifier:CellID];
}
-(void)setUpTopView{
    UIView *topview = [[UIView alloc]init];
    
    self.topView = topview;
    
    topview.frame = CGRectMake(0, 0, 0, 200);
    topview.backgroundColor = [UIColor whiteColor];
    
    
    UIImageView *imageV = [[UIImageView alloc]init];
    imageV.image = [UIImage imageNamed:@"bunnerTu"];
    CGFloat imageVX = kScreenWidth / 2 - 40;
    imageV.frame = CGRectMake(imageVX, 20, 80, 80);
    [topview addSubview:imageV];
    
    
    UILabel *label1 = [[UILabel alloc]init];
    label1.font = [UIFont systemFontOfSize:12];
    label1.textColor = [UIColor lightGrayColor];
    label1.textAlignment = NSTextAlignmentCenter;
    label1.frame = CGRectMake(0, 120, kScreenWidth, 15);
    label1.text = @"购物车空空如也";
    [topview addSubview:label1];
    
    
    UIButton *goShoppingBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    [goShoppingBtn setTitle:@"去逛逛" forState:UIControlStateNormal];
    goShoppingBtn.frame = CGRectMake(imageVX-10, 140, 100, 30);
    goShoppingBtn.backgroundColor = GRJColor(124, 167, 91);
    [goShoppingBtn addTarget:self action:@selector(goShoppingBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [topview addSubview:goShoppingBtn];
    
    self.tableView.tableHeaderView = topview;
}
-(void)goShoppingBtnClick{
    
    //self.tabBarController.selectedIndex = 0;

    YSWebViewController *web = [[YSWebViewController alloc]init];
    [self.navigationController pushViewController:web animated:YES];

}

-(void)setUpBottomView{
    
    // tableView的底部视图 也属于tableView内容一部分
    
    // 创建布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置cell尺寸
    
    layout.itemSize = CGSizeMake(wh, wh+30);
    layout.minimumInteritemSpacing = margin;
    layout.minimumLineSpacing = margin;
    
    // 创建UICollectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, wh*10 + 50*12) collectionViewLayout:layout];
    _collectionView = collectionView;
    
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.backgroundColor = GRJColor(240, 240, 240);
    
    // 注册cell
    [collectionView registerNib:[UINib nibWithNibName:@"YSRecommendedProductsCell" bundle:nil] forCellWithReuseIdentifier:ID];
    
    
    // 设置tableView底部视图
    self.tableView.tableFooterView = collectionView;
    
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 19;
}


#pragma mark - UICollectionViewDelegate
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    YSRecommendedProductsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    return cell;
}

// 点击cell就会调用
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}

-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"dddd"];
 
    return cell;
}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *jingXuanV = [[UIView alloc]init];
    //self.jingXuanV = jingXuanV;
    jingXuanV.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIView *shuxianV = [[UIView alloc]init];
    shuxianV .backgroundColor = GRJColor(40, 175, 175);
    shuxianV.frame  = CGRectMake(15, 3, 3, 20);
    [jingXuanV addSubview:shuxianV];
    
    UILabel *jingpinL = [[UILabel alloc]init];
    jingpinL.frame  =CGRectMake(25, 5, 200, 15);
    jingpinL.text  = @"推荐产品";
    jingpinL.textColor = [UIColor grayColor];
    jingpinL.font = [UIFont systemFontOfSize:14];
    [jingXuanV addSubview:jingpinL];
    
    UIView *underLineView= [[UIView alloc]init];
    underLineView.frame =CGRectMake(0, 25, kScreenWidth, 1);
    underLineView.backgroundColor = [UIColor lightGrayColor];
    underLineView.alpha = 0.3;
    [jingXuanV addSubview:underLineView];
    
    //判断是否是编辑状态  是的话就隐藏
   
    
    
    return jingXuanV;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 26;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

@end
